#!/bin/bash

#
# This file is part of util-linux.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#
#

TS_TOPDIR="${0%/*}/../.."
TS_DESC="MBR - dos mode"

. $TS_TOPDIR/functions.sh
ts_init "$*"

ts_check_test_command "$TS_CMD_FDISK"
ts_check_test_command "$TS_HELPER_MD5"

FDISK_CMD_INIT="x\ni\n0x1\nr\n"
FDISK_CMD_WRITE_CLOSE="w\nq\n"
FDISK_CMD_CREATE_DOSLABEL="o\n" #create dos label
FDISK_CMD_CREATE_PRIMARY="n\np\n1\n\n+1M\n" # create primary partition 1 of size 1MB
FDISK_CMD_CHANGE_PARTTYPE="t\nbf\n" # change partition type to "bf - solaris", this requires that there is only one partition to change
FDISK_CMD_SET_ACTIVE="a\n1\n" # set first partition active
FDISK_CMD_CREATE_EXTENDED="n\ne\n2\n\n+10\n" # create extended partition 2 of size 11 cylinders
FDISK_CMD_CREATE_LOGICAL="n\nl\n\n+1\n" # create next logical partition of size 2 cyl
FDISK_CMD_DELETE_LOGICALS="d\n6\nd\n5\nd\n6\n" # delete middle, head, tail, last partitions
FDISK_CMD_DELETE_PRIMARY="d\n1\n" # delete first primary
FDISK_CMD_DELETE_EXTENDED="d\n2\n" # delete second primary

FDISK_OPTIONS="-C 1024 -c=dos -u=cylinders"

function print_layout {
	echo -ne "\n---layout----------\n" >> $TS_OUTPUT
	$TS_CMD_FDISK ${FDISK_OPTIONS} -x ${TEST_IMAGE_NAME} >> $TS_OUTPUT
	echo -ne   "-------------------\n\n" >> $TS_OUTPUT

    ts_fdisk_clean ${TEST_IMAGE_NAME}
}

#
# Note that fdisk will enlarge the disk image (to 57MB) because the logical
# partitions are out of the original range (10MB).
#
ts_init_subtest "empty-pt"
TEST_IMAGE_NAME=$(ts_image_init 10)
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

echo -e "${FDISK_CMD_CREATE_DOSLABEL}${FDISK_CMD_INIT}${FDISK_CMD_WRITE_CLOSE}" \
	 | $TS_CMD_FDISK --noauto-pt ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "create-primary-partition"
echo -e "${FDISK_CMD_CREATE_PRIMARY}${FDISK_CMD_WRITE_CLOSE}" | \
	$TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "set-partition-type"
echo -e "${FDISK_CMD_CHANGE_PARTTYPE}${FDISK_CMD_WRITE_CLOSE}" | \
	$TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "set-primary-par-active"
echo -e "${FDISK_CMD_SET_ACTIVE}${FDISK_CMD_WRITE_CLOSE}" | \
	$TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "re-create-primary-par"
echo -e "d\n${FDISK_CMD_CREATE_PRIMARY}${FDISK_CMD_WRITE_CLOSE}" | \
	$TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "create-extended-par"
echo -e "${FDISK_CMD_CREATE_EXTENDED}${FDISK_CMD_WRITE_CLOSE}" | \
	$TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "create-logical-par"
echo -e "${FDISK_CMD_CREATE_LOGICAL}${FDISK_CMD_CREATE_LOGICAL}${FDISK_CMD_CREATE_LOGICAL}${FDISK_CMD_CREATE_LOGICAL}${FDISK_CMD_WRITE_CLOSE}" \
	| $TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "delete-logical-par"
echo -e "${FDISK_CMD_DELETE_LOGICALS}${FDISK_CMD_WRITE_CLOSE}" \
	| $TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "create-another-log-par"
echo -e "${FDISK_CMD_CREATE_LOGICAL}${FDISK_CMD_WRITE_CLOSE}" \
	| $TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "delete-primary-par"
echo -e "${FDISK_CMD_DELETE_PRIMARY}${FDISK_CMD_WRITE_CLOSE}" \
	| $TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_init_subtest "delete-extended-par"
echo -e "${FDISK_CMD_DELETE_EXTENDED}${FDISK_CMD_WRITE_CLOSE}" \
	| $TS_CMD_FDISK ${FDISK_OPTIONS} ${TEST_IMAGE_NAME} &> /dev/null
ts_image_md5sum >> $TS_OUTPUT 2>> $TS_ERRLOG

print_layout
ts_finalize_subtest

ts_finalize
